// var movie = require("../model/moiveModel.js");
var usersModel = require("../model/usersModel.js");

exports.showUsers = function(req,res,next){
    usersModel.findAll(function(err,docs){
        if(err){
            return res.send({
                success:"no",
                meg: err
            });
        }

        return res.send(docs)
    })
}

exports.inputData = function(req,res,next){
    res.render('users',{title : 'Add User'});
}


exports.addUsers = function(req,res,next){

    var json=JSON.parse(req.body.editor)
    usersModel.creat(json,function(err){
                if(err){
                    res.send({'success':false,'err':err})
                }else{
                    res.send({'success':true})
                }
            })
   
}


exports.getUsers = function(req,res,next){
    var name = req.params.name;
    usersModel.findByName(name,function(err,docs){

        if(err){
            return res.send({
                success:"no",
                meg: err
            });
        }

        return res.send(docs)
    })
}

exports.deleteUsers = function(req,res,next){
    var name = req.body.name;
    usersModel.deleteByName(name,function(err){
        if(err){
            res.send({'success':false,'err':err})
        }else{

            res.send({
                'success': ' ok',
            
        })
        }
    })
}


exports.updateUsers = function(req,res,next){
    var name = req.params.name;
    var newname = req.params.newname;
    usersModel.updateUsers(name,newname,function(err){
        if(err){
            res.send({'success':false,'err':err})
        }else{
            res.send({'success':true})
        }
    });
};

